<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Upload extends CI_Upload {

	function __construct()
    {
        parent::__construct();
    }
    
	public function validate_upload_path()
	{
		if ($this->upload_path == '')
		{
			$this->set_error('upload_no_filepath');
			return FALSE;
		}

		if (function_exists('realpath') AND @realpath($this->upload_path) !== FALSE)
		{
			$this->upload_path = str_replace("\\", "/", realpath($this->upload_path));
		}

		$this->create_dir($this->upload_path);
		
		if ( ! @is_dir($this->upload_path))
		{
			$this->set_error('upload_no_filepath');
			return FALSE;
		}

		if ( ! is_really_writable($this->upload_path))
		{
			$this->set_error('upload_not_writable');
			return FALSE;
		}

		$this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/",  $this->upload_path);
		return TRUE;
	}
	
	public function create_dir($str_path) {
		$pathArray = explode ( "/", $str_path );
		$path = '';
		for($i = 0; $i < count ( $pathArray ); $i ++) {
			$path = $path . $pathArray [$i] . "/";
			if ( ! file_exists ( $path )) {
				mkdir ( $path );
			}
		}
	}
	
}
